<template>
  <el-dialog :title="action === 'add' ? '新增' : '修改'" width="40%" draggable 
              :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="90px">
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="dataForm.name" minlength="2" maxlength="20" show-word-limit placeholder="品牌名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌logo" prop="logo">
        <list-upload v-model="dataForm.logo" :limit="1"></list-upload>
      </el-form-item>
      <el-form-item label="是否显示" prop="visible">
        <el-switch v-model="dataForm.visible"></el-switch>
      </el-form-item>
      <el-form-item label="品牌排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :min="1" :max="10000" label="品牌排序"></el-input-number>
      </el-form-item>
      <el-form-item label="品牌介绍" prop="desc">
        <el-input type="textarea" v-model="dataForm.desct" maxlength="100" show-word-limit placeholder="品牌介绍"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import admin from "@/utils/admin";
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";
import { ElMessageBox, FormInstance } from "element-plus";

import ListUpload from "@/components/upload/list-upload.vue";

// 操作：add-新增，update-修改
const action = ref("");

// 弹窗是否显示
const visible = ref(false);

// 提交按钮是否禁用
const submitted = ref(false);
// 表单数据
const dataForm = reactive({
  id: undefined,
  name: "",
  logo: "",
  sort: 1,
  desct: "",
  visible: false,
});

// 表单校验规则
const dataRule = reactive({
  name: validate.required("品牌名称"),
  logo: validate.required("品牌logo"),
});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['loadBrandData']);

// 表单初始化
const initForm = async (action, node) => {
  action = action;
  // 新增
  if (action === "add") {
    visible.value = true;
    await nextTick();
    dataForm.id = undefined;
    dataForm.name = "";
    dataForm.logo = "";
    dataForm.sort = 1;
    dataForm.desct = "";
    dataForm.visible = false;
    return;
  }
  // 修改操作
  dataForm.id = node.id;
  http
    .post({
      url: "/product/brand/info",
      params: { brandId: dataForm.id },
    }).then(async ({ data }) => {
      if (http.success(data)) {
        visible.value = true;
        await nextTick();
        dataFormRef.value.resetFields();
        dataForm.id = data.body.id;
        dataForm.name = data.body.name;
        dataForm.logo = data.body.logo;
        dataForm.sort = data.body.sort;
        dataForm.desct = data.body.desct;
        dataForm.visible = data.body.visible === 1;
      }
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: `/product/brand/save`,
          data: {
            id: dataForm.id || undefined,
            name: dataForm.name,
            logo: dataForm.logo,
            sort: dataForm.sort,
            desct: dataForm.desct,
            visible: dataForm.visible ? 1 : 0,
          },
        }).then(({ data }) => {
          if (http.success(data, true, true)) {
            visible.value = false;
            emit("loadBrandData");
            return;
          }
          submitted.value = false;
        });
    }
  });
};

// 暴露方法
defineExpose({ initForm });
</script>
